package com.cheyunkeji.er.activity.fast_evaluate;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.fast_evaluate.CarBrandList;
import com.cheyunkeji.er.bean.fast_evaluate.CarSeries;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class FastEvaluateCarSeriesSelectActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tv_car_brand)
    TextView tvCarBrand;
    @BindView(R.id.lv_car_series_list)
    ListView lvCarSeriesList;
    @BindView(R.id.activity_fast_evaluate_car_series_select)
    LinearLayout activityFastEvaluateCarSeriesSelect;

    @BindView(R.id.ll_car_model_tip)
    LinearLayout ll_car_model_tip;
    @BindView(R.id.tv_car_model_vv)
    TextView tv_car_model_vv;

    private String model_mark;

    private ArrayList<String> carSeriesName;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<CarSeries> carSeriesList;
    private CarBrandList.CarBrand selectedCarBrand;

    private String from = "";
    @Override
    protected void init() {
        setContentView(R.layout.er_activity_fast_evaluate_car_series_select);
        ButterKnife.bind(this);

        if(getIntent().hasExtra("from")) {
            from = getIntent().getStringExtra("from");
        }
        selectedCarBrand = ((CarBrandList.CarBrand) getIntent().getSerializableExtra(Constants.SELECTED_CAR_BRAND_NAME));

        if (null != selectedCarBrand)
            loadCarSeriesData();

    }


    private void loadCarSeriesData() {

        HashMap<String, String> params = new HashMap<>();
//        params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//        params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
        params.put("brandId", selectedCarBrand.getId());
        ApiClient.postForm(HttpConstants.CAR_300_SERIES_LIST, params, new RespCallback<List<CarSeries>>() {

            @Override
            protected void onRequestFailed(String msg) {
                SToast.show(msg);
            }

            @Override
            protected void onSuccess(List<CarSeries> data) {

                carSeriesList = (ArrayList<CarSeries>) data;
                fillData(carSeriesList);
            }


            @Override
            public void onBefore(Request request, int id) {
                showDialog();
            }

            @Override
            public void onAfter(int id) {
                dismissDialog();
            }

        });

    }


    private void fillData(ArrayList<CarSeries> carSeriesList) {

        if (null != carSeriesList) {

            carSeriesName = new ArrayList<>();
            for (int i = 0; i < carSeriesList.size(); i++) {
                carSeriesName.add(carSeriesList.get(i).getName());
            }

            arrayAdapter = new CarSeriesAdapter(this, R.layout.er_fast_evaluate_car_series_lv_item, carSeriesName);
            lvCarSeriesList.setAdapter(arrayAdapter);
            lvCarSeriesList.setOnItemClickListener(this);

        }

    }

    @Override
    protected void initView() {

        vTopbar.setTitle("选择车系");
        vTopbar.setLeftBack();

        if (null != selectedCarBrand)
            tvCarBrand.setText(selectedCarBrand.getName());

        if(getIntent().hasExtra("Mark")){
            model_mark = getIntent().getStringExtra("Mark");
        }
        if (!TextUtils.isEmpty(model_mark)){
            ll_car_model_tip.setVisibility(View.VISIBLE);
            tv_car_model_vv.setText(model_mark);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(this, FastEvaluateCarModelSelectActivity.class);
        intent.putExtra(Constants.SELECTED_CAR_SERIES_NAME,carSeriesList.get(position));
        intent.putExtra("from",from);
        intent.putExtra("Mark", model_mark);
        startActivity(intent);
//        startActivity(new Intent(this,FastEvaluateCarModelSelectActivity.class).putExtra(Constants.SELECTED_CAR_SERIES_NAME,carSeriesList.get(position)));
        MyApplication.getInstance().getCar300Ids().setCarSeriesId(this.carSeriesList.get(position).getId());
    }


    /**
     * 重写getView方法的ArrayAdapter
     */
    public class CarSeriesAdapter extends ArrayAdapter<String> {

        private ArrayList<String> carBrandList;
        private int layoutResId;
        private Context context;

        public CarSeriesAdapter(Context context, int resource, List<String> objects) {
            super(context, resource, objects);
            this.context = context;
            this.carBrandList = (ArrayList<String>) objects;
            this.layoutResId = resource;
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.er_fast_evaluate_car_series_lv_item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (position % 2 != 0) {
                holder.tvCarBrandName.setBackgroundColor(Color.parseColor("#ffffff"));
            } else {
                holder.tvCarBrandName.setBackgroundColor(Color.parseColor("#fbfbfb"));
            }
            holder.tvCarBrandName.setText(carBrandList.get(position));
            return convertView;

        }

        class ViewHolder {
            @BindView(R.id.tv_car_brand_name)
            TextView tvCarBrandName;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }


}
